﻿#include "StretchCommand.h"
#include <GraphicsBoxItem.h>


StretchCommand::StretchCommand(GraphicsBaseItem* item,
	const GraphicsItemInfo& oldItemInfo,
	const QMap<GraphicsBaseItem*, GraphicsItemInfo>& oldChildInfoMap,
	const GraphicsItemInfo& newItemInfo,
	const QMap<GraphicsBaseItem*, GraphicsItemInfo>& newChildInfoMap)
	:BaseCommand()
	, m_item(item)
	, m_oldItemInfo(oldItemInfo)
	, m_oldChildInfoMap(oldChildInfoMap)
	, m_newItemInfo(newItemInfo)
	, m_newChildInfoMap(newChildInfoMap)
{

}

StretchCommand::~StretchCommand()
{

}

void StretchCommand::undo()
{
	m_item->getItemData().setItemScale(m_oldItemInfo.scale);
	m_item->setPos(m_oldItemInfo.pos);
	m_item->getItemData().setScaleSize(m_oldItemInfo.rect.size());
	m_item->refresh();
	for (GraphicsBaseItem* item : m_oldChildInfoMap.keys()) {
		const QPointF oldPos = m_oldChildInfoMap[item].pos;
		const QSizeF oldSize = m_oldChildInfoMap[item].rect.size();
		item->getItemData().setScaleSize(oldSize);
		item->setPos(oldPos);
	}

	if (GraphicsItemType::Box_Enum == m_item->getItemData().getBaseItemType()) {
		GraphicsBoxItem* boxItem = dynamic_cast<GraphicsBoxItem*>(m_item);
		boxItem->fitToScreen();
	}
}

void StretchCommand::redo()
{
	if (m_isFirstPush) {
		m_isFirstPush = false;
		return;
	}
	m_item->getItemData().setItemScale(m_newItemInfo.scale);
	m_item->setPos(m_newItemInfo.pos);
	m_item->getItemData().setScaleSize(m_newItemInfo.rect.size());
	m_item->refresh();
	for (GraphicsBaseItem* item : m_newChildInfoMap.keys()) {
		const QPointF newPos = m_newChildInfoMap[item].pos;
		const QSizeF newSize = m_newChildInfoMap[item].rect.size();
		item->setPos(newPos);
		item->getItemData().setScaleSize(newSize);
	}
	if (GraphicsItemType::Box_Enum == m_item->getItemData().getBaseItemType()) {
		GraphicsBoxItem* boxItem = dynamic_cast<GraphicsBoxItem*>(m_item);
		boxItem->fitToScreen();
	}
}
